package com.happypets.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.happypets.model.EvaluateBean;
import com.happypets.servicesImpl.PetServiceImpl;

public class EvaluateAJAXServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
     
	
	PetServiceImpl service =  new PetServiceImpl();
	
    public EvaluateAJAXServlet() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=UTF-8");
		response.setHeader("Cache-Control", "no-store"); 
		
		String fp = request.getParameter("forwardPage");
		String id = request.getParameter("id");
		
		int showNumber = 3;
		if(fp == null || fp.equals("null")){
			fp = "1";
		}
		
		if(id == null || id.equals("null")){
			id = "1";
		}
		List<EvaluateBean> leb = service.pageEvalutesQuery(Integer.parseInt(id), showNumber , Integer.parseInt(fp));
		String json = new String();
		
		json += "[";
		
		for (int i = 0; i < leb.size(); i++) {
			json += "{";
			EvaluateBean eb = leb.get(i);
			json += "\"name\":\""+eb.getBuyerName()+"\",";
			json += "\"price\":\""+eb.getPrice()+"\",";
			json += "\"content\":\""+eb.getEvaluate()+"\",";
			json += "\"date\":\""+eb.getEvaluateDate()+"\",";
			json += "\"start\":\""+eb.getStart()+"\"}";
			if(i != leb.size()-1){
				json += ",";
			}
			
		}
		json += "]";
		System.out.println(json);
		
		PrintWriter out = response.getWriter();
		
		out.write(json);
		out.flush();
		out.close();
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
